package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import client.ClientBean;

/**
 * 
 * @author aninha
 * 
 */
public class ClientDAO {

	private Connection connection;

	/**
	 * 
	 */
	public ClientDAO() {
		this.connection = new ConnectionFactory().getConnection();
	}

	public void addClient(ClientBean c) {

		String sql = "insert into client (id,name,email) values (?,?,?)";

		try {
			PreparedStatement ps = connection.prepareStatement(sql);

			ps.setString(1, c.getId());
			ps.setString(2, c.getName());
			ps.setString(3, c.getEmail());

			ps.execute();
			ps.close();

		} catch (SQLException e) {
			throw new RuntimeException(e);
		}

	}
	
	public List<ClientBean> getList () {
		
		String sql = "select * from client";
		
		try {
			
			PreparedStatement ps = connection.prepareStatement(sql);
			
			ResultSet rs = ps.executeQuery();
			
			List<ClientBean> list = new ArrayList<ClientBean>();
			
			while (rs.next()) {
				String id = rs.getString("id");
				String name = rs.getString("name");
				String email = rs.getString("email");
				list.add(new ClientBean(id, name, email));
			}
			
			return list;
			
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		
	}

}
